﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Data;

public partial class Evaluate_BaseItemInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "320302"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
        }
        if (!IsPostBack)
        {
            ViewState["Action"] = "Add";
            ActionTitle.Text = Department.GetDepartment(Request.QueryString["DepartmentID"] + GetWhere()).FullName + Request.QueryString["Year"] + "年" + Request.QueryString["Month"] + "月基础数据成绩录入";
            string sql = "select BaseID,BaseName from Base Where Unitid='"+Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID+"' and Flag=0 Order By BaseID";
            DataView dv1 = SplitPage.ExecuteSQL(sql).Tables[0].DefaultView;
            BasesList.DataSource = dv1;
            BasesList.DataTextField = "BaseName";
            BasesList.DataValueField = "BaseID";
            BasesList.DataBind();
            BindGrid();
        }
    }

    private string GetWhere()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return "000";
        }
        else
        {
            //总队人员
            return "0";
          
        }
    }

    private void InitData()
    {
        ItemScore.Text = "";
        Description.Text = "";
    }
    private string GetWhere2()
    {
        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);
        if (Request.QueryString["DepartmentID"] == null)
            return string.Empty;
        string ReturnStr = string.Empty;
        //是否是司政后防管理员
        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            ReturnStr= "Left(DepartmentID,4)=Left('" + Request.QueryString["DepartmentID"] + "',4) ";
        }
        else
        {
            ReturnStr = "Left(DepartmentID,6)=Left('" + Request.QueryString["DepartmentID"] + "',6) ";
        }
        return ReturnStr;
    }
    private void BindGrid()
    {
        string sql = "";
        sql = GetWhere2();
        sql += " and YearValue=" + Request.QueryString["Year"] + " and Monthvalue=" + Request.QueryString["Month"];
        List<BaseItemInfo> items = BaseItem.GetList(sql);	
        UserGrid.DataSource = items;
        UserGrid.DataBind();

    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void UserGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        ViewState["Action"] = "Edit";
        ItemID.Value = UserGrid.DataKeys[e.NewSelectedIndex].Value.ToString();
        BaseItemInfo item = BaseItem.GetBaseItem(Convert.ToInt32(ItemID.Value));
        ItemScore.Text = item.ItemScore.ToString();
        Description.Text = item.Description;

    }
    protected void UserGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ViewState["Action"] = "Add";
        BaseItem.Delete(Convert.ToInt32(UserGrid.DataKeys[e.RowIndex].Value.ToString()));
        BindGrid();
        Prompt.Text = "提示：删除成功！";
    }
    public string GetBaseName(int baseID)
    {
        return Base.GetBase(baseID).BaseName;
    }
    private void Insert()
    {
        BaseItemInfo item = new BaseItemInfo();
        item.BaseID = Convert.ToInt32(BasesList.SelectedValue);
        item.DepartmentID = Request.QueryString["DepartmentID"].ToString()+"0";
        item.ItemScore = decimal.Parse(ItemScore.Text);
        item.Description = Description.Text;
        item.YearValue = Convert.ToInt32(Request.QueryString["Year"]);
        item.MonthValue = Convert.ToInt32(Request.QueryString["Month"]);
        BaseItem.Insert(item);
        Prompt.Text = "提示：添加成功！";
    }
    private void Update()
    {
        BaseItemInfo item = BaseItem.GetBaseItem(Convert.ToInt32(ItemID.Value));
        item.DepartmentID = Request.QueryString["DepartmentID"].ToString();
        item.ItemScore = decimal.Parse(ItemScore.Text);
        item.Description = Description.Text;
        BaseItem.Update(item);
        ViewState["Action"] = "Add";
        Prompt.Text = "提示：更新成功！";
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (ViewState["Action"].ToString() == "Add")
                Insert();
            else
                Update();
            BindGrid();
            InitData();
        }
        catch (Exception ex)
        {
            Prompt.Text = "错误：" + ex.Message;
        }
    }
    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("BaseInputs.aspx");
    }
}
